package com.lee;

import javax.servlet.ServletException;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Article {
    int id;
    User author;
    String title;
    Image image;
    String body;

    public Article(int id, User author, String title, Image image, String body) {
        this.id = id;
        this.author = author;
        this.title = title;
        this.image = image;
        this.body = body;
    }

    public static Article publish(User author, String title, Image image, String body) throws ServletException {
        try (Connection connection = DB.getConnection()) {
            String sql = "INSERT INTO articles (author_id,image_id,title,body) VALUES(?,?,?,?)";
            try (PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
                statement.setInt(1, author.id);
                statement.setInt(2, image.id);
                statement.setString(3, title);
                statement.setString(4, body);

                statement.executeUpdate();
                try (ResultSet resultSet = statement.getGeneratedKeys()) {
                    if (resultSet.next()) {
                        int id = resultSet.getInt(1);
                        return new Article(id, author, title, image, body);
                    } else {
                        return null;
                    }
                }
            }
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }

    public static Article getById(int id) throws ServletException {
        try (Connection connection = DB.getConnection()) {
            String sql = "SELECT title,author_id,users.username as author_username,image_id,body FROM articles,users WHERE articles.author_id=users.id AND articles.id = ?";
            try (PreparedStatement statement = connection.prepareStatement(sql)) {
                statement.setInt(1, id);
                try (ResultSet resultSet = statement.executeQuery()) {
                    if (resultSet.next()) {
                        String title = resultSet.getString("title");
                        int authorId = resultSet.getInt("author_id");
                        String authorUsername = resultSet.getString("author_username");
                        int imageId = resultSet.getInt("image_id");
                        String body = resultSet.getString("body");

                        User author = new User(authorId, authorUsername);
                        Image image = new Image(imageId);
                        return new Article(id, author, title, image, body);
                    } else {
                        return null;
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException(e);
        }
    }

    public static List<Article> list() throws ServletException {
        List<Article> articleList = new ArrayList<>();

        try (Connection connection = DB.getConnection()) {
            String sql = "SELECT " +
                    "articles.id as article_id," +
                    "users.id as author_id," +
                    "users.username as author_username," +
                    "image_id," +
                    "title," +
                    "body" +
                    " FROM articles,users" +
                    " WHERE articles.author_id=users.id " +
                    "ORDER BY articles.id DESC";
            try (PreparedStatement statement = connection.prepareStatement(sql)) {
                try (ResultSet resultSet = statement.executeQuery()) {
                    while (resultSet.next()) {
                        int articleId = resultSet.getInt("article_id");
                        int authorId = resultSet.getInt("author_id");
                        String authorUsername = resultSet.getString("author_username");
                        int imageId = resultSet.getInt("image_id");
                        String title = resultSet.getString("title");
                        String body = resultSet.getString("body");

                        User author = new User(authorId, authorUsername);
                        Image image = new Image(imageId);
                        Article article = new Article(articleId, author, title, image, body);
                        articleList.add(article);
                    }
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new ServletException(e);
        }
        return articleList;
    }
}
